package com.example.rabbitmq.controller.qos;

import com.example.rabbitmq.Constants;
import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class QosQueueListener {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void ListenerQueue(Message message, Channel channel) throws IOException {
        System.out.printf("消费者1:%s, diveryTag:%d\n",new String(message.getBody()),
                message.getMessageProperties().getDeliveryTag());
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,true);
        }
    }

    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void ListenerQueue1(Message message, Channel channel) throws IOException {
        System.out.printf("消费者2:%s, diveryTag:%d\n",new String(message.getBody()),
                message.getMessageProperties().getDeliveryTag());
        try {
            Thread.sleep(100);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,true);
        }
    }
}
